package com.my.swas.sso.module.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Splitter;
import com.my.swas.common.base.ResponseResult;
import com.my.swas.common.utils.BeanCopierUtil;
import com.my.swas.sso.common.base.BaseController;
import com.my.swas.sso.module.admin.entity.App;
import com.my.swas.sso.module.admin.service.AppService;
import com.my.swas.sso.module.cms.dto.CmsCategoryDto;
import com.my.swas.sso.module.cms.entity.CmsCategory;
import com.my.swas.sso.module.cms.service.CmsCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: :MaYong
 */
@Controller
@Slf4j
@RequestMapping("admin/cms/category")
public class CmsCategoryController extends BaseController {
    @Resource
    CmsCategoryService cmsCategoryService;
    @Resource
    AppService appService;

    @ResponseBody
    @PostMapping("list")
    public ResponseResult list(CmsCategoryDto cmsCategoryDto){
        List<App> appList = appService.getAppByCurrentUser();
        List<Long> appIdList = appList.stream().map(App::getId).collect(Collectors.toList());
        CmsCategory cmsCategory = new CmsCategory();
        BeanCopierUtil.convert(cmsCategoryDto,cmsCategory);
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(cmsCategory)
                .in(CollectionUtils.isNotEmpty(appIdList),CmsCategory::getCategoryAppId,appIdList);
        IPage pageInfo = cmsCategoryService.page(cmsCategoryDto.getPage(),queryWrapper);
        return ResponseResult.ok(pageInfo);
    }


    @GetMapping("toSavePage")
    public String toSavePage(@RequestParam(value = "categoryId",required = false) Long categoryId, Model model){
        CmsCategory cmsCategory = cmsCategoryService.getById(categoryId);
        model.addAttribute("res",cmsCategory);
        return "layui/cms/category/add";
    }

    @ResponseBody
    @PostMapping("save")
    public ResponseResult save(CmsCategoryDto cmsCategoryDto){
        CmsCategory cmsCategory = new CmsCategory();
        BeanCopierUtil.convert(cmsCategoryDto,cmsCategory);
        cmsCategoryService.saveOrUpdate(cmsCategory);
        return ResponseResult.ok;
    }


    @ResponseBody
    @GetMapping("getCategoryLayTree")
    public ResponseResult bulidLayTree(){
        return ResponseResult.ok(cmsCategoryService.buildLayTree());
    }

    @ResponseBody
    @GetMapping("getCategoryZTree")
    public ResponseResult bulidZTree(@RequestParam(value = "appId",required = false) Long appId){
        return ResponseResult.ok(cmsCategoryService.buildZTree(appId));
    }

    @ResponseBody
    @GetMapping("currentCategory")
    public ResponseResult getCurrentCategory(@RequestParam(value = "appId",required = false) Long appId){
        return ResponseResult.ok(cmsCategoryService.getCurrentCategory(appId));
    }


    @ResponseBody
    @GetMapping("remove")
    public ResponseResult removeByCategoryId(@RequestParam(value = "ids") String ids){
        List<Long> lists = Splitter.on(",").splitToList(ids).stream()
                .map(s -> Long.valueOf(s))
                .collect(Collectors.toList());
        cmsCategoryService.removeByIds(lists);
        return ResponseResult.ok;
    }



}
